<?php

namespace App\Console\Commands;

use App\Models\Common\Channel\Channel;
use App\Models\Common\Channel\Repositories\ChannelRepository;
use App\Models\Common\Channel\Repositories\Interfaces\ChannelRepositoryInterface;
use App\Models\Payment\ChannelPayment;
use App\Models\Payment\Repositories\Interfaces\PaymentRepositoryInterface;
use Exception;
use Illuminate\Console\Command;

class ChannelManage extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'channel:manage';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '维护app渠道';
    /**
     * @var ChannelRepository
     */
    private mixed $channelRepo;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->channelRepo = app()->make(ChannelRepositoryInterface::class);

    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $question = "请输入渠道管理方式:\n 1.添加渠道\n 2.删除渠道\n 3.查看渠道 4.绑定支付\n请输入数字";
        $resp     = $this->ask($question);
        switch ($resp) {
            case 1:
                $this->addChannel();
                break;
            case 2:
                break;
            case 3:
                $headers  = ['ChannelID', 'Name', 'AppID', 'MchID'];
                $channels = collect(Channel::with('channel_payment')->get())->map(fn($c) => [
                    'channel_id' => $c->channel_id,
                    'name'       => $c->name,
                    'appid'      => $c->appid,
                    'mchid'      => $c->channel_payment?->mchid,
                ])->toArray();
                $this->table($headers, $channels);
                break;
            case 4:
                $this->bindPayment();
                break;
            default:
                $this->error('不支持该操作:'.$resp);
        }
    }

    private function addChannel()
    {
        $appid = trim($this->ask("请输入appid"));
        if ($this->channelRepo->findChannelByAppId($appid)) {
            $this->error("该appid已存在");
            return false;
        }
        $secret  = trim($this->ask("请输入app secret"));
        $name    = trim($this->ask("请输入app名称"));
        $addData = [
            'name'   => $name ?: '',
            'appid'  => $appid,
            'secret' => $secret ?: '',
        ];
        $this->channelRepo->addChannel($addData);
        $this->info('添加成功');
        $headers = ['ChannelID', 'Name', 'AppID'];
        $users   = Channel::all(['channel_id', 'name', 'appid'])->toArray();
        $this->table($headers, $users);
    }

    private function bindPayment()
    {
        $appid = trim($this->ask("请输入appid"));
        $app   = $this->channelRepo->findChannelByAppId($appid);
        if (!$app) {
            $this->error("该appid不存在");
            return false;
        }

        $mchid       = trim($this->ask("请输入支付商户号mchid"));
        $paymentRepo = app(PaymentRepositoryInterface::class);
        $payment     = $paymentRepo->findOnePaymentByMchId($mchid);
        if (!$payment) {
            $this->error("该商户号不存在");
            return false;
        }

        $cp   = new ChannelPayment();
        $bind = $cp->bind($app, $payment);

        if (!$bind) {
            $this->error("绑定失败");
            return false;
        }
        $this->info("绑定成功");
        return true;

    }

    public function deleteChannel()
    {
        $appid = trim($this->ask("请输入appid"));
        try {
            $channel = $this->channelRepo->findChannelByAppId($appid);
        } catch (Exception $exception) {
            $this->error("该appid不存在");
            return false;
        }
        $this->channelRepo->deleteChannel($channel);
        $this->info('删除成功');
    }


}
